/*  GStruct: data structure helpers for Java
 *  Copyright (C) 2011 Giulio Franco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gstruct.sequential.lazy;

import gstruct.single.Pair;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;

/**
 * Collection containing all the pairs formed by coupling
 * elements of two backing collections (cartesian product).
 * 
 * This implementation is lazy, meaning that it does not actually store elements,
 * but generates them on the fly by means of two iterators.
 * As a consequence, iterators returned by this collection may throw
 * {@link ConcurrentModificationException}
 * if one of the backing collections is modified.
 * Also, this collection inherits thread-safety properties from
 * the backing collections.
 * @author Giulio Franco <giulio_franco@msn.com>
 *
 * @param <TypeA>	Type of elements contained in the first collection
 * @param <TypeB>	Type of elements contained in the second collection
 */
public class LazyCartesian<TypeA, TypeB> extends AbstractCollection<Pair<TypeA, TypeB>>{

	private class PairIterator implements Iterator<Pair<TypeA, TypeB>> {
		private Iterator<TypeA> aI = aColl.iterator();
		private TypeA currentAElem;
		private boolean aEnded;
		private Iterator<TypeB> bI;
		
		private PairIterator() {
			switchOff();
		}
		
		private boolean switchOff() {
			if (aI.hasNext()) {
				currentAElem = aI.next();
				bI = bColl.iterator();
				aEnded = false;
				return true;
			} else {
				aEnded = true;
				return false;
			}
		}
		
		@Override
		public boolean hasNext() {
			return (!aEnded && (bI.hasNext() || (switchOff() && bI.hasNext())));
		}

		@Override
		public Pair<TypeA, TypeB> next() {
			if (aEnded || (!bI.hasNext() && !switchOff())) {
				currentAElem = aI.next();		//throws exception
			}
			return new Pair<>(currentAElem, bI.next());
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
		
	}
	
	private final Collection<TypeA> aColl;
	private final Collection<TypeB> bColl;
	
	/**
	 * Creates a cartesian product collection using the given collections. 
	 * @param aColl		First collection
	 * @param bColl		Second collection
	 */
	public LazyCartesian(Collection<TypeA> aColl, Collection<TypeB> bColl) {
		this.aColl = aColl;
		this.bColl = bColl;
	}

	@Override
	public Iterator<Pair<TypeA, TypeB>> iterator() {
		return new PairIterator();
	}

	@Override
	public int size() {
		return aColl.size() * bColl.size();
	}

}
